﻿<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="Grid.aspx.cs" Inherits="EndPointRegistrationWeb.Grid" %>

<html>
    <head>
        <link rel="Stylesheet" href="CSS/Right.css"/>
        <script language="javascript">
        function GridOnMouseOver(linha)
        {
            if(linha.bgColor != "#c0c0c0")
                linha.bgColor = "#f0f0f0";
        }
        
        function GridOnMouseOut(linha)
        {
            if(linha.bgColor != "#c0c0c0")
                linha.bgColor = "#ffffff";
        }
        
        function GridMouseDblClick(dataGrid, linha)
        {
            grid = eval(dataGrid);
            var rows = grid.getElementsByTagName("tr");

            for(i = 0; i < rows.length; i++)
            {
                rows[i].bgColor = "#ffffff";
                inputs = rows[i].getElementsByTagName("input");
                for(j = 0; j < inputs.length; j++)
                {
                    if(inputs[j].type == "checkbox")
                        inputs[j].checked = false;
                }
            }

            linha.bgColor = "#c0c0c0";
            linha.getElementsByTagName("input")[0].checked = true;
            
            tds = linha.getElementsByTagName("td");
            codigoEndPoint = tds[1].firstChild.data;
            modulo = tds[2].firstChild.data;

            submodulo = "";
            
            if (tds[3].firstChild != null)
                submodulo = tds[3].firstChild.data;
                
            contrato = tds[4].firstChild.data;
            protocolo = tds[5].firstChild.data;

            SetParentForm(codigoEndPoint, modulo, submodulo, contrato, protocolo);

            parent.endpointForm.btDeleteSelected.disabled = false;
        }
        
        function GridMouseClick(datagrid, idCheckBox)
        {
            parent.endpointForm.hdnCodigoEndPoint.value = "";
            
			//parent.endpointForm.txtModulo.value = "";
			parent.endpointForm.ddlModulo.value = "";
	
            //parent.endpointForm.txtSubModulo.value = "";
            parent.endpointForm.ddlSubModulo.value = "";
            
            parent.endpointForm.txtContrato.value = "";
            //parent.endpointForm.txtProtocolo.value = "";
            parent.endpointForm.cmbProtocolo.selectedIndex = 0;
        
            grid = eval(datagrid);
            var rows = grid.getElementsByTagName("tr");
            
            for(i = 1; i < rows.length; i++)
            {
                inputs = rows[i].getElementsByTagName("input");
                for(j = 0; j < inputs.length; j++)
                {
                    if(inputs[j].type == "checkbox" && inputs[j].id == idCheckBox)
                    {
                        if(inputs[j].checked)
                        {
                            rows[i].bgColor = "#ffffff";
                            inputs[j].checked = false;
                        }
                        else
                        {
                            rows[i].bgColor = "#c0c0c0";
                            inputs[j].checked = true;
                        }
                        break;
                    }
                }
            }
            
            GetSelectedItems(grid);
        }
        
        function GetSelectedItems(grid)
        {
            parent.endpointForm.hdnSelected.value = "";
        
            var rows = grid.getElementsByTagName("tr");

            for(i = 1; i < rows.length; i++)
            {
                inputs = rows[i].getElementsByTagName("input");
                for(j = 0; j < inputs.length; j++)
                {
                    if(inputs[j].type == "checkbox" && inputs[j].checked)
                    {
                        parent.endpointForm.hdnSelected.value += inputs[j].id + ",";
                    }
                }
            }

			parent.endpointForm.hdnSelected.value = parent.endpointForm.hdnSelected.value.substring(0, parent.endpointForm.hdnSelected.value.length-1);
			if(parent.endpointForm.hdnSelected.value.length == 0)
				parent.endpointForm.btDeleteSelected.disabled = true;
			else
				parent.endpointForm.btDeleteSelected.disabled = false;
        }
        
        function SetParentForm(codigoEndPoint, modulo, submodulo, contrato, protocolo)
        {
            parent.endpointForm.hdnSelected.value = "";
            parent.endpointForm.hdnCodigoEndPoint.value = codigoEndPoint;
			
			//parent.endpointForm.txtModulo.value = modulo;
			//parent.endpointForm.ddlModulo.value = modulo;
            
            var ddl = eval(parent.endpointForm.ddlModulo);
            
            //alert('ddl.length=[' + ddl.options.length + ']'); //debug
            
            for(i=0 ; i < ddl.options.length; i++){

                if(ddl[i].value == modulo){

                    //alert('ddl.options=[' + i + ']'); //debug
                    
                    ddl.options[i].selected = true;

                    break;
                }

            }

            //parent.endpointForm.txtSubModulo.value = submodulo;           
            if (submodulo != "")
                parent.endpointForm.ddlSubModulo.value = submodulo;
            else        
                parent.endpointForm.ddlSubModulo.value = parent.endpointForm.ddlSubModulo.options[1].value;
            
            parent.endpointForm.txtContrato.value = contrato;
            //parent.endpointForm.txtProtocolo.value = protocolo;
            parent.endpointForm.cmbProtocolo.value = protocolo;
        }
        
        function ResetGridColor(datagrid)
        {
            grid = eval(datagrid);
            var rows = grid.getElementsByTagName("tr");
            
            for(i = 1; i < rows.length; i++)
            {
                inputs = rows[i].getElementsByTagName("input");
                for(j = 0; j < inputs.length; j++)
                {
                    if(inputs[j].type == "checkbox")
                    {
                        inputs[j].checked = false;
                        rows[i].bgColor = "#ffffff";
                    }
                }
            }
        }
        </script>
    </head>
    <body leftmargin="0" topmargin="0" rightmargin="0" bottommargin="0">
        <form id="grdForm" runat="server" />
    </body>
</html>